<%-- 
    Document   : Cart
    Created on : 08-Jul-2013, 23:46:55
    Author     : MrMAD
--%>

<%@page import="mybean.Cart"%>
<%@page import="javax.smartcardio.Card"%>
<%@page import="mybean.BookInCart"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page import="mybean.DBProcess"%>
<%@page import="java.util.ArrayList"%>
<%@page import="mybean.Book"%>
<%@page import="java.util.List"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Book Store</title>
        <link rel="stylesheet" href="MyCss/mycss.css">
        <link rel="stylesheet" href="MyCss/cart.css">
    </head>
    <body>
        <script src="js/buy.js"></script>
        <div id="templatemo_content_right" style="width: 100%">
            <div class="title">
                <span class="title_icon">
                    <img id="cart_logo" title="" alt="" src="images/bullet1.jpg">
                </span>
                My cart
            </div>
            <div class="feat_prod_box_details">
                <table class="cart_table">
                    <tbody>
                        <tr class="cart_title">
                            <th>Cover</th>
                            <th>Name</th>
                            <th>Price</th>
                            <th>Quantity</th>
                            <th>Total</th>                            
                        </tr>                        
                        <%
                            Cart cart = new Cart();
                            DBProcess dbProcess = new DBProcess();

                            Cookie cookie = null;
                            Cookie[] cookies = null;

                            cookies = request.getCookies();
                            if (cookies != null) {
                                for (int i = 0; i < cookies.length; i++) {
                                    cookie = cookies[i];
                                    try {
                                        Integer.parseInt(cookie.getName());                           
                                        Book book = dbProcess.getBook(cookie.getName());
                                        int quanti = Integer.parseInt(cookie.getValue());
                                        if(quanti == 0){
                                            continue;
                                        }
                                        BookInCart bookInCart = new BookInCart(book, quanti);
                                        cart.add(bookInCart);
                                    } catch (Exception e) {
                                        continue;
                                    }
                                }
                            }
                            session.setAttribute("totalQuantity", cart.getTotalQuantity());
                            session.setAttribute("totalCash", cart.getTotalCash());
                        %>
                        <c:forEach var="bookInCart" items="<%= cart.getCart()%>">
                            <tr>
                                <td class="bookCover">
                                    <a href="detail.jsp?bookID=${bookInCart.book.bookID}">
                                        <img class="cart_thumb" border="0" title="" alt="cover" src="${bookInCart.book.cover}">
                                    </a>
                                </td>
                                <td class="bookInfo">${bookInCart.book.bookTitle}</td>
                                <td class="bookInfo">${bookInCart.book.price}</td>
                                <td class="bookInfo"><input type="text" id="${bookInCart.book.bookID}" onkeypress="return check_num(this,3,event)" size="3" value="${bookInCart.quantity}" style="text-align:center;"/></td>
                                <td class="bookInfo">${bookInCart.totalCash}</td>
                            </tr>
                        </c:forEach>                                                
                        <tr class="total">                                                      
                            <td class="cart_total" colspan="3">
                                <span class="red">Total</span>
                            </td>
                            <td><%= cart.getTotalQuantity()%></td>                            
                            <td><%= cart.getTotalCash()%></td>
                        </tr>
                    </tbody>
                </table>
                <button class="continue" onclick="back()">< Continue</button>
                <button class="update" onclick="update()">Update</button>
                <button class="remove" onclick="remove()">Remove all</button>
                <button class="checkout" onclick="checkout()">Checkout ></button>
            </div>
            <div class="clear"></div>
        </div>
    </body>
</html>
